package com.mapler.ngfx.ui;

import com.mapler.ui.*;
import com.mapler.utility.HttpHelper;
import com.mapler.utility.IConstant;
import java.awt.BorderLayout;
import java.net.URL;
import javax.swing.JScrollPane;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

/**
 *
 * @author none
 */
public final class MainNGFXUI extends javax.swing.JFrame {

    private String userId;
    private String userType;
    private String username;
    private String password;
    private String country;
    private String is_pva;
    
    private AddNGFXAccUI addNGFXAccUI;
    private ViewNGFXAccUI viewNGFXAccUI;
    private AccNGFXImpUI accNGFXImpUI;
    private NGFXScriptUI ngfxScriptUI;
    
    private AccountViewUI accountViewUI;
    private AddAccessUI addAccessUI;
    
    
    
    private AddSettingUI addSettingUI;
    private EngineNGFXSignupUI engineNGFXSignupUI;
    private EngineNGFXPostUI engineNGFXPostUI;
    private AddAdUI addAdUI;
    private AdViewUI adViewUI;
    private AdImpUI adImpUI;
    
    private JScrollPane jsp;
    
    private static Logger log = Logger.getLogger(MainNGFXUI.class);

    /**
     * Creates new form MainUI
     */
    public MainNGFXUI() {
        initComponents();
        init();
    }

    public MainNGFXUI(String userId, String userType, String username, String password, String is_pva) {
        log.info("Init MainUI");
        this.userId = userId;
        this.userType = userType;
        this.username = username;
        this.password = password;
        this.is_pva = is_pva;
        initComponents();

        if (is_pva.equals("0")) {
            jMICLAddAccount.setVisible(false);
            jMImport.setVisible(false);
        }      
        init();
    }

    @SuppressWarnings("unchecked")
    public void init() {
        addNGFXAccUI = new AddNGFXAccUI(this.getUsername(), this.getPassword(), this.getCountry());
        viewNGFXAccUI = new ViewNGFXAccUI(this.getUsername(), this.getPassword(), this.getCountry());
        accNGFXImpUI = new AccNGFXImpUI(this.getUsername(), this.getPassword(), this.getCountry());
        ngfxScriptUI = new NGFXScriptUI(this.getUsername(), this.getPassword(), this.getCountry());
        
        accountViewUI = new AccountViewUI(this.getUsername(), this.getPassword());
        addAccessUI = new AddAccessUI(this.getUsername(), this.getPassword(), this.getCountry());
        
        
        addSettingUI = new AddSettingUI(this.getUsername(), this.getPassword(), this.getCountry());
        engineNGFXSignupUI = new EngineNGFXSignupUI(this.getUsername(), this.getPassword(), this.getCountry());
        engineNGFXPostUI = new EngineNGFXPostUI(this.getUsername(), this.getPassword(), this.getCountry());
        addAdUI = new AddAdUI(this.getUsername(), this.getPassword(), this.getCountry());
        adViewUI = new AdViewUI(this.getUsername(), this.getPassword(), this.getCountry());
        adImpUI = new AdImpUI(this.getUsername(), this.getPassword(), this.getCountry());
        
    }

   public AccountViewUI getAccountViewUI() {
        return accountViewUI;
    }

    public void setAccountViewUI(AccountViewUI accountViewUI) {
        this.accountViewUI = accountViewUI;
    }

    public AddAccessUI getAddAccessUI() {
        return addAccessUI;
    }

    public void setAddAccessUI(AddAccessUI addAccessUI) {
        this.addAccessUI = addAccessUI;
    }

    public AddSettingUI getAddSettingUI() {
        return addSettingUI;
    }

    public void setAddSettingUI(AddSettingUI addSettingUI) {
        this.addSettingUI = addSettingUI;
    }

    public AddAdUI getAddAdUI() {
        return addAdUI;
    }

    public void setAddAdUI(AddAdUI addAdUI) {
        this.addAdUI = addAdUI;
    }

    public AdViewUI getAdViewUI() {
        return adViewUI;
    }

    public void setAdViewUI(AdViewUI adViewUI) {
        this.adViewUI = adViewUI;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu4 = new javax.swing.JMenu();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMILogout = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMICLAddAccount = new javax.swing.JMenuItem();
        jMICLViewAccount = new javax.swing.JMenuItem();
        jMImport = new javax.swing.JMenuItem();
        jMenu7 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMAdImp = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuEngine = new javax.swing.JMenu();
        jMenuItemShow = new javax.swing.JMenuItem();
        jMIPost = new javax.swing.JMenuItem();

        jMenu4.setText("jMenu4");

        jMenu5.setText("jMenu5");

        jMenuItem6.setText("jMenuItem6");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("Account");

        jMILogout.setText("Logout");
        jMILogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMILogoutActionPerformed(evt);
            }
        });
        jMenu1.add(jMILogout);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("NGFX Account");
        jMenu2.setActionCommand("");

        jMICLAddAccount.setText("Add");
        jMICLAddAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAccessHandler(evt);
            }
        });
        jMenu2.add(jMICLAddAccount);

        jMICLViewAccount.setText("View");
        jMICLViewAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewAccessHandler(evt);
            }
        });
        jMenu2.add(jMICLViewAccount);

        jMImport.setText("Import");
        jMImport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMImportActionPerformed(evt);
            }
        });
        jMenu2.add(jMImport);

        jMenuBar1.add(jMenu2);

        jMenu7.setText("NGFX Ad");

        jMenuItem7.setText("add");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAdHandler(evt);
            }
        });
        jMenu7.add(jMenuItem7);

        jMenuItem8.setText("view");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewAdHandler(evt);
            }
        });
        jMenu7.add(jMenuItem8);

        jMAdImp.setText("Import");
        jMAdImp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMAdImpActionPerformed(evt);
            }
        });
        jMenu7.add(jMAdImp);

        jMenuBar1.add(jMenu7);

        jMenu3.setText("Scripts");

        jMenuItem4.setText("Add");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addScriptHandler(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuBar1.add(jMenu3);

        jMenu6.setText("Settings");

        jMenuItem5.setText("Add");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSettingHandler(evt);
            }
        });
        jMenu6.add(jMenuItem5);

        jMenuBar1.add(jMenu6);

        jMenuEngine.setText("Engine");
        jMenuEngine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showEngineHandler(evt);
            }
        });

        jMenuItemShow.setText("Signup");
        jMenuItemShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showEngineHandler(evt);
            }
        });
        jMenuEngine.add(jMenuItemShow);

        jMIPost.setText("Post");
        jMIPost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMIPostActionPerformed(evt);
            }
        });
        jMenuEngine.add(jMIPost);

        jMenuBar1.add(jMenuEngine);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 732, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 411, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clear() {
        if (jsp != null ) {
            this.remove(jsp);
        }
        addNGFXAccUI.setVisible(false);
        viewNGFXAccUI.setVisible(false);
        accNGFXImpUI.setVisible(false);
        
        accountViewUI.setVisible(false);
        addAccessUI.setVisible(false);
        
        ngfxScriptUI.setVisible(false);
        addSettingUI.setVisible(false);
        engineNGFXSignupUI.setVisible(false);
        engineNGFXPostUI.setVisible(false);
        addAdUI.setVisible(false);
        adViewUI.setVisible(false);
        adImpUI.setVisible(false);
        
    }
    private void addAccessHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAccessHandler
        this.clear();
        addNGFXAccUI.clearFields();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(addNGFXAccUI, BorderLayout.CENTER);
        addNGFXAccUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_addAccessHandler

    private void viewAccessHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewAccessHandler
        this.clear();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(viewNGFXAccUI, BorderLayout.CENTER);
        viewNGFXAccUI.getAccounts();
        viewNGFXAccUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_viewAccessHandler

    private void addScriptHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addScriptHandler
        this.clear();
        ngfxScriptUI.clearFields();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(ngfxScriptUI, BorderLayout.CENTER);
        ngfxScriptUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_addScriptHandler

    private void addSettingHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSettingHandler
        this.clear();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(addSettingUI, BorderLayout.CENTER);
        addSettingUI.getSetting();
        addSettingUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_addSettingHandler

    private void showEngineHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showEngineHandler
        this.clear();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(engineNGFXSignupUI, BorderLayout.CENTER);
        engineNGFXSignupUI.initSettings();
        engineNGFXSignupUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_showEngineHandler

    private void addAdHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAdHandler
        this.clear();
        addAdUI.clearFields();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(addAdUI, BorderLayout.CENTER);
        addAdUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_addAdHandler

    private void viewAdHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewAdHandler
        this.clear();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(adViewUI, BorderLayout.CENTER);        
        adViewUI.getAdvertisement();
        adViewUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_viewAdHandler

    private void jMILogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMILogoutActionPerformed
        try {
            String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=ngfxuser/logout&username=" + this.username + "&password=" + this.password;
            String response = HttpHelper.post(new URL(uri), "");

            Document document = DocumentHelper.parseText(response);
            String errorCode = document.valueOf("Response/errorcode");
            if (errorCode.equalsIgnoreCase("000")) {
                String msg = document.valueOf("Response/message");
                log.debug("Problem in logout. causes " + msg);
            } else {
                LoginNGFXUI dialog = new LoginNGFXUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                this.dispose();
                dialog.setVisible(true);
            }
        } catch (Throwable ex) {
            log.debug("Problem in logout. causes " + ex);
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jMILogoutActionPerformed

    private void jMAdImpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMAdImpActionPerformed
        adImpUI.clearFields();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(adImpUI, BorderLayout.CENTER);
        this.clear();
        adImpUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_jMAdImpActionPerformed

    private void jMImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMImportActionPerformed
        accNGFXImpUI.clearFields();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(accNGFXImpUI, BorderLayout.CENTER);
        this.clear();
        accNGFXImpUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_jMImportActionPerformed

    private void jMIPostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMIPostActionPerformed
        this.clear();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(engineNGFXPostUI, BorderLayout.CENTER);
        engineNGFXPostUI.initSettings();
        engineNGFXPostUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_jMIPostActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainNGFXUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jMAdImp;
    private javax.swing.JMenuItem jMICLAddAccount;
    private javax.swing.JMenuItem jMICLViewAccount;
    private javax.swing.JMenuItem jMILogout;
    private javax.swing.JMenuItem jMIPost;
    private javax.swing.JMenuItem jMImport;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuEngine;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItemShow;
    // End of variables declaration//GEN-END:variables
}
